<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ page import="net.fckeditor.*"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="com.travelsky.airchina.boarding.domain.NewsColumn"%>
<%@page import="com.travelsky.airchina.boarding.common.ConstType"%>
<%@ include file="/common/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>管理中心 - 添加新闻</title>
	<%@ include file="/common/meta.jsp" %>
	<link href="${ctx}/css/system/general.css" rel="stylesheet" type="text/css" />
	<link href="${ctx}/css/system/main.css" rel="stylesheet" type="text/css" />
	<link href="${ctx}/css/system/category.css" rel="stylesheet" type="text/css" />	
	<script type="text/javascript" src="${ctx}/js/checkFrameSet.js"></script>
	<script type="text/javascript" src="${ctx}/js/jquery-1.4.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/json.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/newscategory.js"></script>
	<script language="JavaScript">
	 var contextPath = "${ctx}";
	 $(document).ready(findCategoryList);
	 // 获取编辑器中HTML内容
	 function getEditorHTMLContents(EditorName) {
		 var oEditor = FCKeditorAPI.GetInstance(EditorName);
		 return(oEditor.GetXHTML(true));
	 }
	 // 获取编辑器中文字内容
	 function getEditorTextContents(EditorName) {
		 var oEditor = FCKeditorAPI.GetInstance(EditorName);
		 return(oEditor.EditorDocument.body.innerText);
	 }
	 // 设置编辑器中内容
	 function setEditorContents(EditorName, ContentStr) {
		 var oEditor = FCKeditorAPI.GetInstance(EditorName);
		 oEditor.SetHTML(ContentStr);
	 }	
	 function stripHtml(value) {
		// remove html tags and space chars
		return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
		// remove numbers and punctuation
		.replace(/[0-9.(),;:!?%#$'"_+=\/-]*/g,'');
	 }
	 
	 function splitEditorContents(oEditorTextContents,perPageSum,pageSeparator){
	     var pOriginalContents = oEditorTextContents.split("</p>");
	     var contents = "";
	     if(pOriginalContents!=null && pOriginalContents.length>0){
	         var pContentsLength = pOriginalContents.length;
	         var sum = 0;
	         var pageContent = "";
	         for(i=0;i<pContentsLength;i++){
	            pContent = stripHtml(pOriginalContents[i]);
	            if(sum == 0){
		            if(pContent.length<perPageSum){
		               sum += pContent.length;
		               if(pOriginalContents[i].indexOf("<p>")!=-1){
		                  pageContent += pOriginalContents[i]+"</p>";
		               }else{
		                  pageContent += pOriginalContents[i];
		               }
		               if(sum>=perPageSum){
		                   sum = 0;
		                   if(i<pContentsLength-1){
		                       contents += pageContent+pageSeparator;
		                   }else{
		                       contents += pageContent;
		                   }
		                   pageContent = "";
		               }
		            }else{
		               if(pOriginalContents[i].indexOf("<p>")!=-1){
		                   if(i<pContentsLength-1){
		                       contents += pOriginalContents[i]+"</p>"+pageSeparator;
		                   }else{
		                       contents += pOriginalContents[i]+"</p>";
		                   }
		               }else{
		                   if(i<pContentsLength-1){
		                       contents += pOriginalContents[i]+pageSeparator;
		                   }else{
		                       contents += pOriginalContents[i];
		                   }
		               }
		            }
	            }else{
	                sum += pContent.length;
	                if(pOriginalContents[i].indexOf("<p>")!=-1){
	                   pageContent += pOriginalContents[i]+"</p>";
	                }else{
	                   pageContent += pOriginalContents[i];
	                }
	                if(sum>=perPageSum){
	                   sum = 0;
	                   if(i<pContentsLength-1){
	                       contents += pageContent+pageSeparator;
	                   }else{
	                       contents += pageContent;
	                   }
	                   pageContent = "";
	                }
	            }
	         }
	         if(pageContent!=""){
	             contents += pageContent;
	         }
	     }
	     return contents;
	 }
	 function setNewsContentTemp(editorName){
	     if($("#autoPage").val()=="1"){
		     var newsContent = getEditorHTMLContents(editorName);
		     if(newsContent!=""){
			     var newsContentSum = stripHtml(getEditorHTMLContents(editorName)).length;
				 var perPageSum = $("#perPageSum").val();
			     if(parseInt(newsContentSum) > parseInt(perPageSum)){
				     var newsContentTemp = newsContent.replace(new RegExp('<%=ConstType.NEWS_PAGE_SEPARATOR%>','gm'),'');
				     setEditorContents(editorName,splitEditorContents(newsContentTemp,perPageSum,'<%=ConstType.NEWS_PAGE_SEPARATOR%>'));
			     }
		     }
	     }
	 }
	 function changeAutoPage(isAutoPage){
	     if(isAutoPage=="1"){
	        $("#perPageSumSpan").show();
	     }else{
	        $("#perPageSumSpan").hide();
	     }
	 }
	 function onSubmint(btIdVar){
	      if(submitOperation(btIdVar,true)){
		      setNewsContentTemp("content");
		      document.getElementById("theForm").target = "_self";
		      document.getElementById("theForm").action = "${ctx}/system/news";
		      setTimeout("submitNews()",1000);
	      }
	 }
	 function previewNews(btIdVar){
	      if(submitOperation(btIdVar,false)){
		      setNewsContentTemp("content");
		      document.getElementById("theForm").target = "_blank";
		      document.getElementById("theForm").action = "${ctx}/system/news/preview";
		      setTimeout("submitNews()",1000);
	      }
	 }
	 function submitNews(){
	      document.getElementById("theForm").submit();
	 }
	 function submitOperation(btIdVar,disabledVar){
	    if(disabledVar==true){
	       document.getElementById(btIdVar).disabled = true;
	    }
	    var isValid = true;
	    if($.trim($("#title").val())==""){
	       $("#newsTitleTip").show();
	       window.location.hash = "aNewsTitleTip";
	       isValid = false;
	    }else{
	       $("#newsTitleTip").hide();
	    }
	    if($("#p_cate_id").val()==""){
	       $("#newsColumnTip").show();
	       if(isValid){
	          window.location.hash = "aNewsColumnTip";
	       }
	       isValid = false;
	    }else{
	       $("#newsColumnTip").hide();
	    }
	    if($("#video").val()!=""){
	       var videoFileName = $("#video").val();
           var pos = videoFileName.lastIndexOf(".");
           var lastname = videoFileName.substring(pos,videoFileName.length) 
	       if(lastname.toLowerCase()!=".flv"){
		       $("#videoNewsTip").show();
		       if(isValid){
		          window.location.hash = "aVideoNewsTip";
		       }
		       isValid = false;
	       }else{
	           $("#videoNewsTip").hide();
	       }
	    }else{
	       $("#newsColumnTip").hide();
	    }
	    if(getEditorHTMLContents("content")==""){
	       $("#newsContentTip").show();
	       if(isValid){
	          window.location.hash = "aNewsContentTip";
	       }
	       isValid = false;
	    }else{
	       $("#newsContentTip").hide();
	    }
	    if(!isValid==true){
	       if(disabledVar==true){
	          document.getElementById("submitBt").disabled = false;
	       }
	       return false;
	    }else{
	       return true;
	    }
	 }
	</script>
	<%
		FCKeditor fckEditor = new FCKeditor(request, "content");
		fckEditor.setHeight("560");
		String desc = (String)request.getAttribute("content");
		fckEditor.setValue(desc!=null?desc:"");
	%>
</head>
<body>
<h1>
<span class="action-span"><a href="${ctx}/system/news/manage">新闻列表</a></span>
<span class="action-span1"><a href="#">管理中心</a>  - 添加新闻 </span>
<div style="clear:both"></div>
</h1>

<div class="main-div">
  <form:form modelAttribute="news" action="${ctx}/system/news" method="post" name="theForm" id="theForm" enctype ="multipart/form-data">
  <input type="hidden" name="p_cate_id" id="p_cate_id" value="${newsColumnId}"/>
  <input type="hidden" name="is_leaf" id="is_leaf" value=""/>
  <table width="100%" id="general-table">
      <tr id="newsTitleTip" style="display:none">
        <td class="label"></td>
        <td class="news-operation-tip"><a name="aNewsTitleTip">请输入新闻标题</a></td>
      </tr>
      <tr>
        <td class="label"><form:label for="title" path="title" cssErrorClass="error">新闻标题</form:label></td>
        <td>
          <form:input path="title" maxlength="50" size="50" /> <font color="red">*</font> <form:errors path="title" /></td>
      </tr>
      <tr>
        <td class="label"><form:label for="subtitle" path="subtitle" cssErrorClass="error">新闻副标题</form:label></td>
        <td><form:input path="subtitle" maxlength="50" size="50" /></td>
      </tr>
      <tr>
        <td class="label"><form:label for="author" path="author" cssErrorClass="error">新闻作者</form:label></td>
        <td><form:input path="author" maxlength="20" size="50" /></td>
      </tr>
      <tr>
        <td class="label"><form:label for="source" path="source" cssErrorClass="error">新闻来源</form:label></td>
        <td><form:input path="source" maxlength="50" size="50" /></td>
      </tr>
      <tr id="newsColumnTip" style="display:none">
        <td class="label"></td>
        <td class="news-operation-tip"><a name="aNewsColumnTip">请选择新闻栏目</a></td>
      </tr>
      <tr>
        <td class="label">新闻栏目</td>
        <td>
	       <div class="cascading-container">
		    <ol id="J_OlCascadingList">
		           <c:choose>
		              <c:when test="${hasError==true}">
		      <li id="l_1" class="root">
		        <ul id="u_1">
		           <c:if test="${rootNewsColumns!=null}">
			           <c:forEach items="${rootNewsColumns}" var="itemTemp">
			             <c:choose>
			                <c:when test="${itemTemp.isLeaf==0}">
					           <c:choose>
					             <c:when test="${rootNewsColumnId==itemTemp.id}">
			           <li id="${itemTemp.id}" class="parent selected"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
					             </c:when>
					             <c:otherwise>
			           <li id="${itemTemp.id}" class="parent"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
					             </c:otherwise>
					           </c:choose>
			                </c:when>
			                <c:otherwise>
					           <c:choose>
					             <c:when test="${rootNewsColumnId==itemTemp.id}">
			           <li id="${itemTemp.id}" class="selected"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
					             </c:when>
					             <c:otherwise>
			           <li id="${itemTemp.id}"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
					             </c:otherwise>
					           </c:choose>
			                </c:otherwise>
			             </c:choose>
			           </c:forEach>
		           </c:if>
		        </ul>
		      </li>
		      
		      <%
		      @SuppressWarnings("unchecked")
		      List<Long> cateIdList = (List<Long>)request.getAttribute("newsColumnIds");
		      Long newsColumnId = (Long)request.getAttribute("newsColumnId");
		      @SuppressWarnings("unchecked")
		      Map<Long,List<NewsColumn>> parentCateListMap = (Map<Long,List<NewsColumn>>)request.getAttribute("map");
		      if(parentCateListMap!=null && parentCateListMap.keySet().size()>0){
		         int liIndex = 0;
		         for(Long pId:parentCateListMap.keySet()){
		            liIndex++;
		       %>
		                <li id="_o_l_<%=liIndex%>">
		                  <ul id="_o_l_u_<%=liIndex%>">
		       <%
		            List<NewsColumn> cateList = parentCateListMap.get(pId);
		            for(NewsColumn cate:cateList){
		                     if("0".equals(cate.getIsLeaf())){
		                        if(cateIdList.contains(cate.getId())){
		                    %>
		                    <li id="<%=cate.getId()%>" class="parent selected"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                        }else{
		                           if(cate.getId().equals(newsColumnId)){
		                    %>
		                    <li id="<%=cate.getId()%>" class="parent selected"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                           }else{
		                    %>
		                    <li id="<%=cate.getId()%>" class="parent"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                           }
		                        }
		                     }else{
		                        if(cateIdList.contains(cate.getId())){
		                    %>
		                    <li id="<%=cate.getId()%>" class="selected"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                        }else{
		                           if(cate.getId().equals(newsColumnId)){
		                    %>
		                    <li id="<%=cate.getId()%>" class="selected"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                           }else{
		                    %>
		                    <li id="<%=cate.getId()%>"><span><%=cate.getName()%></span><input type="hidden" id="columnTypeId<%=cate.getId()%>" value="<%=cate.getColumnType()%>"/></li>
		                    <%
		                           }
		                        }
		                     }
		          }
		      %>
		                  </ul>
		                </li>
		      <%
		         }
		      }
		      %>
		              </c:when>
		              <c:otherwise>
		      <li id="l_1" class="root">
		        <ul id="u_1">
		           <c:if test="${newsColumnList!=null}">
			           <c:forEach items="${newsColumnList}" var="itemTemp">
			             <c:choose>
			                <c:when test="${itemTemp.isLeaf==0}">
			           <li id="${itemTemp.id}" class="parent"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
			                </c:when>
			                <c:otherwise>
			           <li id="${itemTemp.id}"><span>${itemTemp.name}</span><input type="hidden" id="columnTypeId${itemTemp.id}" value="${itemTemp.columnType}"/></li>
			                </c:otherwise>
			             </c:choose>
			           </c:forEach>
		           </c:if>
		        </ul>
		      </li>
		              </c:otherwise>
		           </c:choose>
		    </ol>
		  </div>
        </td>
      </tr>
      
      <tr id="imageTextDiv" style="display:none;">
        <td class="label">新闻图片</td>
        <td>
          <input type="file" id="image" name="image"/>
        </td>
      </tr>
      
      <tr id="videoNewsTip" style="display:none">
        <td class="label"></td>
        <td class="news-operation-tip"><a name="aVideoNewsTip">请选择.flv文件</a></td>
      </tr>
      
      <tr id="videoNewsDiv" style="display:none;">
        <td class="label">视频文件</td>
        <td>
          <input type="file" id="video" name="video"/>
        </td>
      </tr>
      
      <tr>
        <td class="label">是否自动分页</td>
        <td>
           <select name="autoPage" id="autoPage" onchange="javascript:changeAutoPage(this.value);">
              <option value="1">是</option>
              <option value="0">否</option>
           </select>&nbsp;&nbsp;<span id="perPageSumSpan">每页<input type="text" size="10" id="perPageSum" name="perPageSum" value="2000"/>字(不足每页字数时不分页)</span>
        </td>
      </tr>
      
      <tr>
        <td class="label">排序</td>
        <td>
          <form:input path="sortOrder" size="15" /></td>
      </tr>

      <tr id="newsContentTip" style="display:none">
        <td class="label"></td>
        <td class="news-operation-tip"><a name="aNewsContentTip">请输入新闻内容</a></td>
      </tr>
      
      <tr>
        <td class="label">新闻内容</td>
        <td>
		<form:errors path="content" />
        <%out.println(fckEditor);%>
        </td>
      </tr>
      </table>
      <div class="button-div">
        <input type="button" onclick="javascript:onSubmint('submitBt');" id="submitBt" value=" 确定 " />
        <input type="button" onclick="javascrpt:previewNews('previewBt');" id="previewBt" value=" 预览 " />
        <!-- 
        <input type="button" onclick="javascrpt:setNewsContentTemp('content')" value=" 自动分页 " />
         -->
      </div>
  </form:form>
</div>
</body>
</html>